var TARGET_SAVE_URL 	= baseurl + '/target_save.php';
var TARGET_DELETE_URL	= baseurl + '/target_save.php';


function TargetEdit(action){
	action = $(action);
	var container = action.parent().parent().parent().parent();
	container.addClass('edit');
	
	var target = {
		name: container.find('.target-name').text(),
		tags: []
	}
	container.find('.tag_box_frame .tag_box').each(function(){
		target.tags.push(_parseTagLink($(this)));
	});
	
	var html = container.html();
	container.empty();
	var original = $('<div class="original" />').html(html).appendTo(container);
	
	var parameterized_name = parameterize(target.name);
	
	var form = $('<form id="'+parameterized_name+'" class="target-edit" action="javascript:void(0)" onsubmit="TargetSave(this)" method="post" />').appendTo(container);
	form.append('<input type="hidden" name="save_as" class="save_as" value="0" />')
	form.append('<input type="text" class="large" name="name" value="'+target.name+'" />');
	form.append('<div class="tag-search"><input type="text" class="search-tags" /></div>');
	form.append('<div class="tags"></div>');
	
	var buttons = $('<div class="buttons" />').appendTo(form);
	buttons.append('<input type="submit" onclick="$(this).parent().parent().find(\'.save_as\').val(0);" value="Save" class="button" />');
	buttons.append('<input type="submit" onclick="$(this).parent().parent().find(\'.save_as\').val(1);" value="Save as New target" class="button" /><br />');
	buttons.append('<input type="button" onclick="TargetDelete(this, \''+escape(target.name)+'\');" value="Delete" class="button" />');
	buttons.append('<input type="button" onclick="TargetCancelEdit(this);" value="Cancel" class="button" />');
	
	InitTagsBox("form#"+parameterized_name+".target-edit input.search-tags", "input", TargetAddTag);
	
	$(target.tags).each(function(){
		form.find(".tags").append(_makeTagHTML(this, parameterized_name + 'targetedit', 'span'));
	});
	
	return false;
}

function TargetAddTag(a){
	var tag = _parseTagLink(a);
	var c = a.parent().parent().parent().parent();
	var target_parameterized_name = c.attr("id");
	if(!$("#"+target_parameterized_name+"targetedittag-"+tag.type+"-"+tag.id).length){
		var t = _makeTagHTML(tag,target_parameterized_name+'targetedit', 'span');
		t.hide();
		c.find('.tags').append(t);
		t.fadeIn("slow");
	}
}

function TargetSave(form){
	$.ajax({
		url: TARGET_SAVE_URL,
		data: $(form).serialize(),
		success: function(data){
			alert(data);
		}
	})
}

function TargetDelete(btn, target){
	if(confirm('The Target will be delete permanently.\nAre you sure you want to continue?')){
		$.ajax({
			url: TARGET_DELETE_URL,
			data: {delete_target: target},
			error: function(req,status,error){
				alert(req.responseText);
			},
			success: function(data, status, req){
				alert(req.responseText);
				location.reload();
			}
		});
	}
}

function TargetCancelEdit(btn){
	var c = $(btn).parent().parent().parent();
	c.html(c.find('.original').html());
	c.removeClass('edit');
}


function parameterize(string){
	string = string.replace(/[^a-z0-9]/ig, "_");
	return string.replace(/_{2,}/g, "_");
}